package com.cse.bpass;

import java.util.HashMap;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;

public class Instruction extends Activity implements OnTouchListener, TextToSpeech.OnInitListener, OnUtteranceCompletedListener {
	protected TextToSpeech tts;	
	private Button[] buttons;
	private long lastTouch = -1;
	
	private String startInstr = "Start function works only if you have set up the passcode. " +
			"Once you pressed the Start button, find the starting corner of passcode by finger. " +
			"If you find it, tap the screen with another finger while the first finger remains on the screen. " +
			"It will help you if you touch the frame of the phone while drawing the passcode. " +
			"After finished your passcode, tap the screen again with second finger. If your input is correct, it will " +
			"move to phone main menu. If your input is incorrect try again from searching the starting corner with " +
			"a finger and repeat the rest steps until you make the correct input.";
	private String settingInstr ="If this is your first time setting the passcode. First search the starting corner of the " +
			"passcode. If you find it, tap the screen with another finger while the first finger remains on the screen. " +
			"It will help you if you touch the frame of the phone while drawing the passcode. " +
			"After finished your passcode, tap the screen again with second finger. Then, you have to confirm your passcode " +
			"by input the same passcode again. You can search the starting corner by finger. If you are ready, tap the screen " +
			"with another finger. After finished your passcode, and tap with seconde finger. If your confirm is incorrect, it will " +
			"ask you to create the new passcode again and confirm it. If your trying to replace the current passcode, input " +
			"the previous passcode and if you have correct previous passcode, you can make new one. Once you create the new one, you " +
			"have to confirm it by repeating your passcode. After finished setting passcode, the application will go back to B Pass main " +
			"menu.";
	private String intro ="There are three buttons in Instruction page. Top one is a button for instruction for Start function, middle one is a " +
			"button for instruction for setting function, and the last button is for going back to B Pass main menu. You can search the button " +
			"by tapping the screen and it will tell you which button you just tapped. You use want to use the button, double tap the screen.";
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.instruction);
        
        //Create TextToSpeech
        tts = new TextToSpeech(this,this);
        tts.setLanguage(Locale.US);
        
        //Initialize Buttons
        buttons = new Button[3];
        findButtonViews();
        setListeners();
    }
	
	public void onInit(int arg0) {
		tts.speak("Instruction button clicked", TextToSpeech.QUEUE_FLUSH,null);
		tts.speak(intro, TextToSpeech.QUEUE_ADD,null);
	}

	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_UP){
			long currentTime = System.currentTimeMillis();
			if(currentTime - lastTouch < 250){
				lastTouch = -1;				
				switch(v.getId()){
				case R.id.Start_Button:
					tts.speak(startInstr, TextToSpeech.QUEUE_FLUSH, null);
					break;
				case R.id.Setting_Button:		
					tts.speak(settingInstr, TextToSpeech.QUEUE_FLUSH, null);
					break;	
				case R.id.Main_Button:
						tts.setOnUtteranceCompletedListener(this);
						HashMap<String, String> hashUtter = new HashMap<String, String>();
						hashUtter.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "completed");
						tts.speak("Main menu button clicked, Back to B pass main menu",
								TextToSpeech.QUEUE_FLUSH, hashUtter);
						break;
				}
				
			}else{
				if(v.getId() == R.id.Start_Button){
					tts.speak("This is a start button", TextToSpeech.QUEUE_FLUSH, null);
				}else if(v.getId() == R.id.Setting_Button){
					tts.speak("This is a setting button", TextToSpeech.QUEUE_FLUSH, null);
				}else{
					tts.speak("This is a main menu button", TextToSpeech.QUEUE_FLUSH, null);
				}
				lastTouch = currentTime;
			}
		}
		return false;
	}
	
	private void findButtonViews(){
		Display dis = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int height = dis.getHeight();
		
		buttons[0] = (Button)findViewById(R.id.Start_Button);
		buttons[1] = (Button)findViewById(R.id.Setting_Button);
		buttons[2] = (Button)findViewById(R.id.Main_Button);
		
		buttons[0].setHeight(height/3);
		buttons[1].setHeight(height/3);
		buttons[2].setHeight(height/3);
	}
	
	private void setListeners(){
		for(int i = 0; i < buttons.length; i++){
			buttons[i].setOnTouchListener(this);
		}
	}

    @Override
    public void onDestroy() {
        tts.stop();
        tts.shutdown();

        super.onDestroy();
    }

	public void onUtteranceCompleted(String utteranceId) {
		if(utteranceId.equals("completed")){
			Intent intent = new Intent();
			intent.setClass(this, BPassActivity.class);
			startActivity(intent);
		}
	}
}
